/**
 * Created by zzmhot on 2017/3/23.
 *
 * 路由Map
 *
 * @author: zzmhot
 * @github: https://github.com/zzmhot
 * @email: zzmhot@163.com
 * @Date: 2017/3/23 18:30
 * @Copyright(©) 2017 by zzmhot.
 *
 */

import Vue from 'vue'
import VueRouter from 'vue-router'
import store from 'store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

//import components
//view page warp component
import viewPageComponent from 'pages/App'

//home
import homeComponent from 'pages/home'
//404
import noPageComponent from 'pages/error/404'
//login
import loginComponent from 'pages/user/login'
//pages
import {appManage,areaManage,roleManage,orgManage as baseOrgManage ,
        orgManage_eqRepairManage, orgManage_1,
        appManage_eqRepairManage, appManage_1,
        areaManage_eqRepairManage, areaManage_1,
        roleManage_eqRepairManage, roleManage_1,
      } from 'pages/baseManage'

import { orgManage,teacherManage,educationDepartment,chineseTeacher} from 'pages/stdtRegManage'
import { basicsSetup,categoryManage,columnManage,calculationRuleManage,factorManage,
         questionnaireManage,activityManage,answerSheetManage,itemBankManage,
         reportFormManage,statisticsByActivity, statisticsByAnswer,statisticsByQuestionnaire,generalSurveyPDF
        } from 'pages/psychCensusManage'
import {teacherManage as conteacherManage} from 'pages/consultantManage'
import {newMsg} from 'pages/msgManage'
import { feedBack,onlineService} from 'pages/customServiceManage'
import { englishArea,mathArea,physicsArea} from 'pages/learningExchange'

Vue.use(VueRouter)

//使用AMD方式加载
// component: resolve => require(['pages/home'], resolve),
const routes = [{
  path: '/404',
  name: 'notPage',
  component: noPageComponent
}, {
  path: '*',
  redirect: '/404'
}, {
  path: '/user/login',
  name: 'login',
  component: loginComponent
}, {
  path: '/',
  redirect: '/home',
  component: viewPageComponent,
  children: [{
    path: '/home',
    name: 'home',
    component: homeComponent,
    meta: {
      title: "主页",
      auth: true
    }
  },

//导航路由：
//基础管理平台
  {
    path: '/baseManage',
    redirect: '/baseManage/areaManage',
  },
//1.区域管理
  {
    path: '/baseManage/areaManage',
    name: 'areaManage',
    component: areaManage,
    meta: {
      title: "区域管理",
      auth: true
    }
  },
  {
    path:'/baseManage/areaManage/1',
    name: 'areaManage_1',
    component: areaManage_1,
    meta: {
      title: "| 区域管理 > 机构管理1",
      auth: true
    }
  }, 
  {
    path: '/baseManage/areaManage/eqRepairManage',
    name: 'areaManage_eqRepairManage',
    component: areaManage_eqRepairManage,
    meta: {
      title: "| 区域管理 >设备保修管理",
      auth: true
    }
  },
//2.应用管理
  {
    path: '/baseManage/appManage',
    name: 'appManage',
    component: appManage,
    meta: {
      title: "应用管理",
      auth: true
    }
  },

  {
    path:'/baseManage/appManage/1',
    name: 'appManage_1',
    component: appManage_1,
    meta: {
      title: "| 应用管理 >机构管理1",
      auth: true
    }
  },
  {
    path: '/baseManage/appManage/eqRepairManage',
    name: 'appManage_eqRepairManage',
    component: appManage_eqRepairManage,
    meta: {
      title: "| 应用管理 >设备保修管理",
      auth: true
    }
  },
//3.角色管理平台
  {
    path: '/baseManage/roleManage',
    name: 'roleManage',
    component: roleManage,
    meta: {
      title: "角色动态",
      auth: true
    }
  },
  {
    path:'/baseManage/roleManage/1',
    name: 'roleManage_1',
    component: roleManage_1,
    meta: {
      title: "| 角色动态 >机构管理1",
      auth: true
    }
  },
  {
    path: '/baseManage/roleManage/eqRepairManage',
    name: 'roleManage_eqRepairManage',
    component: roleManage_eqRepairManage,
    meta: {
      title: "| 角色动态 >设备保修管理",
      auth: true
    }
  },

//4.机构管理
  {
    path: '/baseManage/orgManage',
    name: 'orgManage',
    component: baseOrgManage,
    meta: {
      title: "机构管理",
      auth: true
    }
  },
  {
    path:'/baseManage/orgManage/1',
    name: 'orgManage_1',
    component: orgManage_1,
    meta: {
      title: "| 机构管理 >机构管理1",
      auth: true
    }
  },
  {
    path: '/baseManage/orgManage/eqRepairManage',
    name: 'orgManage_eqRepairManage',
    component: orgManage_eqRepairManage,
    meta: {
      title: "| 机构管理 >设备保修管理",
      auth: true
    }
  },

//学籍管理平台：
  {
    path: '/stdtRegManage',
    redirect: '/stdtRegManage/orgManage',
  },
//1.机构管理
  {
    path: '/stdtRegManage/orgManage',
    name: 'orgManage',
    component: orgManage,
    meta: {
      title: "机构管理",
      auth: true
    }
  },
  {
    path:'/stdtRegManage/orgManage/educationDepartment',
    name: 'educationDepartment',
    component: educationDepartment,
    meta: {
      title: "| 机构管理 >教育部门",
      auth: true
    }
  },
//2.老师管理
  {
    path: '/stdtRegManage/teacherManage',
    name: 'teacherManage',
    component: teacherManage,
    meta: {
      title: "教师管理",
      auth: true
    }
  },
  {
    path:'/stdtRegManage/teacherManage/chineseTeacher',
    name: 'chineseTeacher',
    component: chineseTeacher,
    meta: {
      title: "| 教师管理 >语文老师",
      auth: true
    }
  },

//心理普查平台
  {
    path: '/psychCensusManage',
    redirect: '/psychCensusManage/basicsSetup',
  },
  //1.基础设置
  {
    path: '/psychCensusManage/basicsSetup',
    name: 'basicsSetup',
    component: basicsSetup,
    meta: {
      title: "基础设置",
      auth: true
    }
  },
  {
    path: '/psychCensusManage/basicsSetup/columnManage',
    name: 'columnManage',
    component: columnManage,
    meta: {
      title: "栏目管理",
      auth: true
    }
  },
  {
    path: '/psychCensusManage/basicsSetup/categoryManage',
    name: 'categoryManage',
    component: categoryManage,
    meta: {
      title: "类别管理",
      auth: true
    }
  },
  {
    path: '/psychCensusManage/basicsSetup/calculationRuleManage',
    name: 'calculationRuleManage',
    component: calculationRuleManage,
    meta: {
      title: "计算规则管理",
      auth: true
    }
  },
  {
    path: '/psychCensusManage/basicsSetup/factorManage',
    name: 'factorManage',
    component: factorManage,
    meta: {
      title: "因子管理",
      auth: true
    }
  },
//2.问卷管理
{
  path: '/psychCensusManage/questionnaireManage',
  name: 'questionnaireManage',
  component: questionnaireManage,
  meta: {
    title: "问卷管理",
    auth: true
  }
},
{
  path: '/psychCensusManage/questionnaireManage/itemBankManage',
  name: 'itemBankManage',
  component: itemBankManage,
  meta: {
    title: "题库管理",
    auth: true
  }
},
{
  path: '/psychCensusManage/questionnaireManage/activityManage',
  name: 'activityManage',
  component: activityManage,
  meta: {
    title: "活动管理",
    auth: true
  }
},
{
  path: '/psychCensusManage/questionnaireManage/answerSheetManage',
  name: 'answerSheetManage',
  component: answerSheetManage,
  meta: {
    title: "答卷管理",
    auth: true
  }
},
//报表管理
{
  path: '/psychCensusManage/reportFormManage',
  name: 'reportFormManage',
  component: reportFormManage,
  meta: {
    title: "报表管理",
    auth: true
  }
},
{
  path: '/psychCensusManage/reportFormManage/statisticsByActivity',
  name: 'statisticsByActivity',
  component: statisticsByActivity,
  meta: {
    title: "按活动统计",
    auth: true
  }
},
{
  path: '/psychCensusManage/reportFormManage/statisticsByAnswer',
  name: 'statisticsByAnswer',
  component: statisticsByAnswer,
  meta: {
    title: "按问卷统计",
    auth: true
  }
},
{
  path: '/psychCensusManage/reportFormManage/statisticsByQuestionnaire',
  name: 'statisticsByQuestionnaire',
  component: statisticsByQuestionnaire,
  meta: {
    title: "按答卷统计",
    auth: true
  }
},
{
  path: '/psychCensusManage/reportFormManage/generalSurveyPDF',
  name: 'generalSurveyPDF',
  component: generalSurveyPDF,
  meta: {
    title: "普查PDF",
    auth: true
  }
},
  //咨询师管理平台
  {
    path: '/consultantManage',
    redirect: '/consultantManage/conteacherManage',
  },
  {
    path: '/consultantManage/conteacherManage',
    name: 'conteacherManage',
    component: conteacherManage,
    meta: {
      title: "咨询师管理",
      auth: true
    }
  },

  //消息管理平台：
  {
    path: '/msgManage',
    redirect: '/msgManage/newMsg',
  },
  {
    path: '/msgManage/newMsg',
    name: 'newMsg',
    component: newMsg,
    meta: {
      title: "最新消息",
      auth: true
    }
  },

  //客服管理平台：
  {
    path: '/customServiceManage',
    redirect: '/customServiceManage/feedBack',
  },
  {
    path: '/customServiceManage/feedBack',
    name: 'feedBack',
    component: feedBack,
    meta: {
      title: "客户留言",
      auth: true
    }
    },
    {
      path: '/customServiceManage/onlineService',
      name: 'onlineService',
      component: onlineService,
      meta: {
        title: "在线服务",
        auth: true
      }
    },
  //学习交流管理平台：
  {
    path: '/learningExchange',
    redirect: '/learningExchange/mathArea',
  },
  {
    path: '/learningExchange/mathArea',
    name: 'mathArea',
    component: mathArea,
    meta: {
      title: "数学专区",
      auth: true
    }
    },
    {
      path: '/learningExchange/englishArea',
      name: 'englishArea',
      component: englishArea,
      meta: {
        title: "英语专区",
        auth: true
      }
    },
    {
      path: '/learningExchange/physicsArea',
      name: 'physicsArea',
      component: physicsArea,
      meta: {
        title: "物理专区",
        auth: true
      }
    },

  ]
}]

const router = new VueRouter({
  routes,
  mode: 'hash', //default: hash ,history
  scrollBehavior (to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return {x: 0, y: 0}
    }
  }
})

//全局路由配置
//路由开始之前的操作
router.beforeEach((to, from, next) => {
  NProgress.done().start()
  let toName = to.name
  // let fromName = from.name
  let is_login = store.state.user_info.login

  if (!is_login && toName !== 'login') {
    next({
      name: 'login'
    })
  } else {
    // if (is_login && toName === 'login') {
    //   next({
    //     path: '/'
    //   })
    // } else {
      next()
    // }
  }
})

//路由完成之后的操作
router.afterEach(route => {
  NProgress.done()
})

export default router
